package tq.com.easysurfacecapture.Inner;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PointF;
import android.print.PrinterId;
import android.util.SizeF;

import tq.com.systools.system.FileHelper;
import tq.com.tqcom.net.TQPictureUpload;

public class TQPictureHold {
    private String path;
    private Bitmap mBitmap;
    private Matrix mMatrix = new Matrix();
    private SizeF parentSize ;
    private SizeF picSize ;

    public TQPictureHold(String picPath){
        if(FileHelper.isFileExists(picPath)){
            mBitmap = BitmapFactory.decodeFile(picPath);
        }else if(picPath != null && picPath.length() > 0){
        }

    }

    public TQPictureHold(Bitmap pBitmap){
        mBitmap = pBitmap;
    }

    public TQPictureHold(Bitmap pBitmap, SizeF picSize){
        mBitmap = pBitmap;
        this.picSize = picSize;
    }

    public void setParentSize(SizeF pSize){
        parentSize = pSize;
    }

    public void draw(Canvas pCanvas, Paint pPaint, PointF offset){
        try {
            if(mBitmap != null){
                pCanvas.drawBitmap(mBitmap, mkMatrix(offset), pPaint);
            }

        }catch (Exception pE){
            pE.printStackTrace();
        }


    }

    private Matrix mkMatrix(PointF offset){
        float scale = 1.0f;
        float x = 0;
        float y = 0;

        Matrix lMatrix = new Matrix();
        if(mBitmap.getWidth() > parentSize.getWidth()){
            scale = parentSize.getHeight()/mBitmap.getWidth();
            lMatrix.setScale(scale, scale);
        }else{
            x = (parentSize.getWidth() - mBitmap.getWidth())/2.0f;
        }

        lMatrix.setTranslate(x, offset.y);

        return lMatrix;
    }
}
